// 请求路径-测试环境
// var getUrl = "http://139.196.255.197:7070";

var getUrl = "http://wxmall.bingstar.com.cn"
// 图片地址路径
var imgUrl = "http://dev-bucket.bingstar.com.cn";
//用户id
var memberId = "1";

/**
 * 请求头信息
 *
 * @param arg1
 * @returns {{key: string}}
 */
function headers(arg1) {
  var headera = {
    "key":"8dddf423-e573-4783-a733-8f8d46cf16a9"
  };
  var sign = "KEY" + headera.key +",METHOD"+arg1 + ",TIMESTAMP" + nowTime();
  var md5s = hex_md5(sign);
  headera.method = arg1;
  headera.timestamp = nowTime();
  headera.sign = md5s;
  headera.token = localStorage.getItem("token");
  return headera;
}

/**
 * 获取url token 参数
 *
 * @param name 参数名
 * @returns {null} 参数值
 * @constructor
 */
function GetQueryString(name) {
  var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if(r!=null)return decodeURI(r[2]); return null;
}

/**
 * 存储token 到本地
 */
function saveToken() {
  var tokens = GetQueryString("token");
  if(tokens != null){
    localStorage.setItem("token", GetQueryString("token"));
  }
  var tximg = GetQueryString("img");
  if(tximg != null){
    localStorage.setItem("tximg", GetQueryString("img"));
  }
  var nick = GetQueryString("nick");
  if(nick != null){
    localStorage.setItem("nick", GetQueryString("nick"));
  }
}

/**
 * 获取当前时间
 * 年月日
 * @returns {string}
 */
var nowTime = function () {
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = year + "" + month +"" + strDate;
  return currentdate;
}

/**
 * 获取微信支付config 信息
 *
 */
function getInitConfig(){
  var hurl = window.location.href.split('#')[0];
  // $.ajax({
  //   type: 'GET',
  //   url: getUrl + '/wxmall/wx/getWxjsdkConfig?requrl='+encodeURIComponent(hurl),
  //   dataType: 'json',
  //   timeout: 15000,
  //   headers: headers("wx.getWxjsdkConfig"),
  //   success: function(resp){
  //     if(resp.success)
  //       wxconfig(resp.data);
  //     else
  //       alert(resp.msg);
  //   },
  //   error: function(xhr, type){
  //     alert("wxjsdkConfig22：请检测网络是否通畅！");
  //   }
  // });
}

/**
 * 配置config
 *
 * @param _jsignPackage
 */
function wxconfig(_jsignPackage){
  wx.config({
    debug: false,
    appId: _jsignPackage.appId,
    timestamp: _jsignPackage.timestamp,
    nonceStr: _jsignPackage.nonceStr,
    signature:_jsignPackage.signature,
    jsApiList: [
      "chooseWXPay",
      "onMenuShareTimeline",
      "onMenuShareAppMessage",
      "onMenuShareQQ",
      "onMenuShareWeibo",
      "onMenuShareQZone",
      "chooseImage"
    ]
  });
}

function test(){
  wx.config({
    debug: true,
    appId:'${appId}',
    timestamp: '${timestamp}',
    nonceStr: '${nonceStr }',
    signature: '${signature}',
    jsApiList: [
      'checkJsApi',
      'onMenuShareTimeline',
      'onMenuShareAppMessage',
      'onMenuShareQQ',
      'onMenuShareWeibo',
      'hideMenuItems',
      'showMenuItems',
      'hideAllNonBaseMenuItem',
      'showAllNonBaseMenuItem',
      'translateVoice',
      'startRecord',
      'stopRecord',
      'onRecordEnd',
      'playVoice',
      'pauseVoice',
      'stopVoice',
      'uploadVoice',
      'downloadVoice',
      'chooseImage',
      'previewImage',
      'uploadImage',
      'downloadImage',
      'getNetworkType',
      'openLocation',
      'getLocation',
      'hideOptionMenu',
      'showOptionMenu',
      'closeWindow',
      'scanQRCode',
      'chooseWXPay',
      'openProductSpecificView',
      'addCard',
      'chooseCard',
      'openCard'
    ]
  });
}

/**
 * 发起支付
 *
 * @param xjson 服务端返回
 */
function preinvokes(xjson){
  if (typeof WeixinJSBridge == "undefined"){
    if( document.addEventListener ){
      document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
    }else if (document.attachEvent){
      document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
      document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
    }
  }else{
    onBridgeReady(xjson);
  }
}

/**
 * 支付成功与否提示信息
 *
 * @param xjson
 */
function onBridgeReady(xjson){
  WeixinJSBridge.invoke(
      'getBrandWCPayRequest',
      xjson,
      function(res){
        // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
        if(res.err_msg == "get_brand_wcpay_request:ok" ) {
          alert("支付成功");
        }else{
          alert("支付失败");
        }
      }
  );
}